package com.xtkj.superclub_shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xtkj.commons.SnowflakeIdWorker;
import com.xtkj.superclub_entity.entity.shop.*;
import com.xtkj.superclub_shop.dao.*;
import com.xtkj.superclub_shop.service.IProService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author lizheqi
 * @create 2019-12-25-15:28
 */
@Service
public class ProServiceImpl implements IProService {

    @Autowired
    private IProDao proDao;
    @Autowired
    private IProTypeDao proTypeDao;
    @Autowired
    private IProColorSizeDao proColorSizeDao;
    @Autowired
    private IColorDao colorDao;
    @Autowired
    private ISizeDao sizeDao;

    //查询上架商品
    @Override
    public List<Product> getGroundPros() {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        //先查询上架商品中的热门商品
        wrapper.eq("hot",0).eq("ground",1);
        List<Product> hotPros = proDao.selectList(wrapper);
        //查询上架商品里面的非热门商品
        wrapper = new QueryWrapper<>();
        wrapper.eq("hot",1).eq("ground",1);
        List<Product> misses = proDao.selectList(wrapper);
        hotPros.addAll(misses);
        return hotPros;
    }

    //查询所有一级分类
    @Override
    public List<ProType> getEpcType() {
        QueryWrapper<ProType> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        return proTypeDao.selectList(wrapper);
    }

    //查询一级分类下面的所有二级分类
    @Override
    public List<ProType> getEpcChildTypes(String epcId) {
        QueryWrapper<ProType> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",epcId);
        return proTypeDao.selectList(wrapper);
    }

    //查询一级分类下面的所有商品
    @Override
    public List<Product> getEpcPros(String epcId) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("epc_id", epcId);
        return proDao.selectList(wrapper);
    }

    //查询二级分类下面的所有商品
    @Override
    public List<Product> getEpcChildPros(String epcChildId) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("epc_child_id", epcChildId);
        return proDao.selectList(wrapper);
    }

    //根据商品id查询商品的详细信息
    @Override
    public List<ProColorSizeMap> getProByProId(String proId) {
        QueryWrapper<ProColorSizeMap> wrapper = new QueryWrapper<>();
        wrapper.eq("pro_id", proId);
        //可能有问题,查询出来的是该商品proId的所有商品
        List<ProColorSizeMap> proColorSizes = proColorSizeDao.selectList(wrapper);
        //通过proId查询商品信息
        Product product = proDao.selectById(proId);
        for (ProColorSizeMap proColorSizeMap:proColorSizes){
            //查询商品proId对应的颜色
            Color color = colorDao.selectById(proColorSizeMap.getColorId());
            //查询商品proId对应的尺寸
            Size size = sizeDao.selectById(proColorSizeMap.getSizeId());
            proColorSizeMap.setSize(size);
            proColorSizeMap.setColor(color);
            proColorSizeMap.setProduct(product);
        }
        return proColorSizes;
    }

}
